package com.xinke.module.system.service.dict;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.xinke.module.system.model.vo.dict.*;
import com.xinke.module.system.model.entity.dict.DictTypeDO;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.util.object.BeanUtils;

import com.xinke.module.system.mapper.dict.DictTypeMapper;

import static com.xinke.component.common.exception.util.ServiceExceptionUtil.exception;
import static com.xinke.module.system.enums.ErrorCodeConstants.*;

/**
 * 字典类型 Service 实现类
 *
 * @author yzx
 */
@Service
@Validated
public class DictTypeServiceImpl implements DictTypeService {

    @Resource
    private DictTypeMapper dictTypeMapper;

    @Override
    public Long createDictType(DictTypeSaveReqVO createReqVO) {
        // 插入
        DictTypeDO dictType = BeanUtils.toBean(createReqVO, DictTypeDO.class);
        dictTypeMapper.insert(dictType);
        // 返回
        return dictType.getTypeId();
    }

    @Override
    public void updateDictType(DictTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateDictTypeExists(updateReqVO.getTypeId());
        // 更新
        DictTypeDO updateObj = BeanUtils.toBean(updateReqVO, DictTypeDO.class);
        dictTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictType(Long dtId) {
        // 校验存在
        validateDictTypeExists(dtId);
        // 删除
        dictTypeMapper.deleteById(dtId);
    }

    private void validateDictTypeExists(Long dtId) {
        if (dictTypeMapper.selectById(dtId) == null) {
            throw exception(null);
        }
    }

    @Override
    public DictTypeDO getDictType(Long dtId) {
        return dictTypeMapper.selectById(dtId);
    }

    @Override
    public PageResult<DictTypeDO> getDictTypePage(DictTypePageReqVO pageReqVO) {
        return dictTypeMapper.selectPage(pageReqVO);
    }

}